package com.wzt.lianfirecontrol.adapter.yuyueweibao;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.App;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.function.yuyueweibao.YuYueModel;
import com.wzt.lianfirecontrol.bean.recode.user.RoleConstant;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

import java.util.List;

public class YuYueListAdapter extends BaseRecyclerAdapter<YuYueModel> {

    private Context context;

    private boolean needSetTopMargin = false;

    public boolean isNeedSetTopMargin() {
        return needSetTopMargin;
    }

    public void setNeedSetTopMargin(boolean needSetTopMargin) {
        this.needSetTopMargin = needSetTopMargin;
    }

    public YuYueListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_yuyueweibao_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, YuYueModel data) {
        if (viewHolder instanceof MyViewHolder) {
            setItemData((MyViewHolder) viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_item_content;
        private TextView tv_title;
        private TextView tv_status;
        private TextView tv_location;
        private LinearLayout ll_person;
        private TextView tv_person;
        private LinearLayout ll_company;
        private TextView tv_company;
        private TextView tv_person_phone;
        private LinearLayout ll_time;
        private TextView tv_time;
        private LinearLayout ll_post_order;
        private TextView tv_post_order;


        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.ll_person = itemView.findViewById(R.id.ll_person);
        myViewHolder.ll_company = itemView.findViewById(R.id.ll_company);
        myViewHolder.tv_company = itemView.findViewById(R.id.tv_company);
        myViewHolder.tv_person = itemView.findViewById(R.id.tv_person);
        myViewHolder.tv_person_phone = itemView.findViewById(R.id.tv_person_phone);
        myViewHolder.ll_time = itemView.findViewById(R.id.ll_time);
        myViewHolder.tv_time = itemView.findViewById(R.id.tv_time);
        myViewHolder.ll_post_order = itemView.findViewById(R.id.ll_post_order);
        myViewHolder.tv_post_order = itemView.findViewById(R.id.tv_post_order);
    }

    private void setItemData(MyViewHolder viewHolder, YuYueModel data, int position) {

        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (0 == position && needSetTopMargin) {
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 15),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15));
        } else {
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 0),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15f));
        }
        if (StringUtils.isEmpty(data.getEquName())) {
            viewHolder.tv_title.setVisibility(View.GONE);
        } else {
            viewHolder.tv_title.setVisibility(View.VISIBLE);
            viewHolder.tv_title.setText(data.getEquName());
        }
        viewHolder.ll_post_order.setVisibility(View.GONE);

        if (StringUtils.isEmpty(data.getStatus())) {
            viewHolder.tv_status.setVisibility(View.GONE);
        } else {
            viewHolder.tv_status.setVisibility(View.VISIBLE);
            GradientDrawable gradientDrawable = new GradientDrawable();
            if ("1".equals(data.getStatus())) {
                viewHolder.tv_status.setText("待处理");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
                UserInfoModel userInfoModel = UserInfoModel.getUserInfo(context);
                // 只有维保人员显示
                if (userInfoModel.getRoleAlias().contains(RoleConstant.ROLE_REPAIR_MANAGER)) {
                    viewHolder.ll_post_order.setVisibility(View.VISIBLE);
                    viewHolder.tv_post_order.setText("派单");
                    viewHolder.ll_post_order.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 派单
                            Bundle bundle = new Bundle();
                            bundle.putString(ConstData.SHOWTYPPE, ConstData.WEIBAOPERSON);
                            bundle.putSerializable("yuyueModel", data);
                            ((BaseActivity) context).setClickAction(ConstData.SHOWFRAG, ConstData.INFOLIST, "维保指派", null, bundle);
                        }
                    });
                }
            } else if ("2".equals(data.getStatus())) {
                viewHolder.tv_status.setText("维保中");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.weibaozhong_color));


            } else if ("3".equals(data.getStatus())) {
                viewHolder.tv_status.setText("确认中");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
            } else {
                viewHolder.tv_status.setText("已完成");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
                UserInfoModel userInfoModel = UserInfoModel.getUserInfo(context);

                // 非维保人员显示
                if (!userInfoModel.getRoleAlias().contains(RoleConstant.ROLE_REPAIR_MANAGER)
                        && !userInfoModel.getRoleAlias().contains(RoleConstant.ROLE_REPAIR)
                        && "0".equals(data.getAppraise())) {
                    //   viewHolder.ll_post_order.setVisibility(View.VISIBLE);
                    List<String> alias = UserInfoModel.getUserInfo(App.context).getRoleAlias();

                    if (alias.contains(RoleConstant.ROLE_ORG_MANAGER)) {
                        viewHolder.ll_post_order.setVisibility(View.GONE);
                    } else {
                        viewHolder.ll_post_order.setVisibility(View.VISIBLE);

                    }
                    viewHolder.tv_post_order.setText("评价");
                    viewHolder.ll_post_order.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 评价
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("yuyueModel", data);
                            ((BaseActivity) context).setClickAction(ConstData.SHOWFRAG, ConstData.EVALUATE, "评价", null, bundle);
                        }
                    });
                }
            }
            gradientDrawable.setCornerRadius(Utils.dip2px(context, 10));
            viewHolder.tv_status.setBackground(gradientDrawable);
        }

        StringBuilder stringBuilder = new StringBuilder();
        if (!StringUtils.isEmpty(data.getBuildingName())) {
            stringBuilder.append(data.getBuildingName());
        }
        if (!StringUtils.isEmpty(data.getFloorName())) {
            stringBuilder.append(data.getFloorName());
        }
        if (!StringUtils.isEmpty(data.getAddress())) {
            stringBuilder.append(data.getAddress());
        }
        if (StringUtils.isEmpty(stringBuilder.toString())) {
            viewHolder.tv_location.setVisibility(View.GONE);
        } else {
            viewHolder.tv_location.setVisibility(View.VISIBLE);
            viewHolder.tv_location.setText(stringBuilder.toString());
        }

        if (StringUtils.isEmpty(data.getReporter())) {
            viewHolder.ll_person.setVisibility(View.GONE);
        } else {
            viewHolder.ll_person.setVisibility(View.VISIBLE);
            viewHolder.tv_person.setText(data.getReporter());
            if (StringUtils.isEmpty(data.getReporterPhone())) {
                viewHolder.tv_person_phone.setVisibility(View.GONE);
            } else {
                viewHolder.tv_person_phone.setVisibility(View.VISIBLE);
                viewHolder.tv_person_phone.setText(data.getReporterPhone());
            }
        }
        if (StringUtils.isEmpty(data.getCompanyName())) {
            viewHolder.ll_company.setVisibility(View.GONE);
        } else {
            viewHolder.ll_company.setVisibility(View.VISIBLE);
            viewHolder.tv_company.setText(data.getCompanyName());
        }
        if (StringUtils.isEmpty(data.getCreateTime())) {
            viewHolder.ll_time.setVisibility(View.GONE);
        } else {
            viewHolder.ll_time.setVisibility(View.VISIBLE);
            viewHolder.tv_time.setText(data.getCreateTime());
        }


    }
}
